09. Exercise: Interfaces
Exercise: Interfaces
Create the Vehicle
interface
Task Description:
Let's get some practice with interfaces. First, follow these steps to create an interface named Vehicle
:
Task Feedback:
Nice work!
Create a Car
subclass
Task Description:
Next, you will be creating a subclass to the Vehicle
interface named Car
:
Task Feedback:
Nice work!
Solution
ND079 C1 L3 A07b Interface Solution
Here is the solution for creating the Vehicle
interface. You might have noticed that when you created the Car
subclass and implemented the Vehicle
interface that InteliJ gave you an error. The error should have been describing that you need to override the Vehicle
methods.
One other thing to note is that that Interfaces are types just like classes are types!
public interface Vehicle {
public String getType();
public String getSpeed();
public String getColor();
}
public class Car implements Vehicle {
private String type;
private String speed;
private String color;
public Car(String type, String speed, String color) {
super();
this.type = type;
this.speed = speed;
this.color = color;
}
@Override
public String getType() {
return type;
}
@Override
public String getSpeed() {
return speed;
}
@Override
public String getColor() {
return color;
}
}
In the solution video I demonstrated how classes can implement more than one interface. Here is the Production interface I used in the video. This was not required for the exercise, I just wanted to show you for demonstration purposes.
public interface Production {
public String location;
}